#ifndef __FILE_MAN_H
#define __FILE_MAN_H

#include "onlinestor.h"
#include <vector>
#include <map>
using namespace std;

struct OnlineFile;
struct DirectoryCache;
struct FileCache;

class FileManager
{
public:
	FileManager();
	void addOnlineStorage(OnlineStorage* stor);
	int connect();
	void disconnect();
	struct OnlineFile* open(const char* pathname);
	int read(struct OnlineFile* file,char* buffer,int offset,int size);
	int write(struct OnlineFile* file,char* buffer,int offset,int size);
	void close(struct OnlineFile* file);
	int del(const char* pathname);
	int truncate(const char* pathname,int size);
	int mkdir(const char* path,const char* name);
	struct NetFile* lsdir(const char* path,OUT int* size /* numbner of struct NetFile */);
	int rmdir(const char* path,const char* name);
	int move(const char* oldFile,const char* newFile);
	int fileinfo(const char* pathname,int* size,int* folder);
	
private:
	int ensure_dir(const char* path);
	map<string,struct DirectoryCache*>::iterator find_dir(const char* path);
	int ensure_block(struct FileCache* f,int block);
	vector<OnlineStorage*> vecStors;
	bool connected;
	static const int blockSize = 128*1024;
	map<string,struct DirectoryCache*> directoryCache;
	void flush(struct FileCache* f);
};

#endif